//
//  MessageSettingVC.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/5.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "MessageSettingVC.h"
#import "MicroDef.h"
#import "TextSwitchCell.h"
#import "CUserSettingModel.h"
#import "CUserSettingMgr.h"
#import "CXmppRequestMgr.h"
//#import "CAccountMgr.h"
//#import "CAccountModel.h"
#import "CFolderMgr.h"

#define KSECTIONHEIGHT 20.0f
@interface MessageSettingVC ()<UITableViewDelegate, UITableViewDataSource, TextSwitchCellDelegate>
{
    UITableView *_tableView;
}

@property (nonatomic, strong) CUserSettingModel *setting;
@property (nonatomic, strong) CAccountModel *acc;
@end

@implementation MessageSettingVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPSETNOTICESETTING object:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    // Do any additional setup after loading the view.
     CUserSettingMgr *setMgr = [[CUserSettingMgr alloc] init];
    self.setting = [[[CUserSettingMgr alloc] init] getUserSetting];
    self.acc = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
    if (self.setting == nil) {
        self.setting = [[CUserSettingModel alloc] init];
        self.setting.userCode = self.acc.userName;
        [setMgr insetUserWithSetting:self.setting];

    }

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveSetting) name:K_XMPPSETNOTICESETTING object:nil];
    
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitleCenter:@"消息通知"];
    
    
    _tableView = [[UITableView alloc] init];
    _tableView.frame = CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT - 64.0f);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    
       
    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSInteger rows = 0;
    
    switch (section) {
        case 0:
            rows = 2;
            break;
        case 1:
            rows = 1;
            break;

        default:
            rows = 0;
            break;
    }
    return rows;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView              // Default is 1 if not implemented
{
    return 4;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SettingCell";
//    DLog(@"section:%zd, rows:%zd", indexPath.section, indexPath.row);
    UITableViewCell *cell = nil;
    TextSwitchCell  *textSwtichCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (textSwtichCell == nil) {
        textSwtichCell = [[[NSBundle mainBundle] loadNibNamed:@"TextSwitchCell" owner:self options:nil] objectAtIndex:0];
    }
    textSwtichCell.indexPath = indexPath;
    textSwtichCell.delegate = self;
    if(indexPath.section == 0 && indexPath.row == 0)
    {
        [textSwtichCell.seperatorLine setHidden:NO];
        textSwtichCell.switchController.on = self.setting.Voice;
    }
    else if (indexPath.section == 0 && indexPath.row == 1)
    {
        [textSwtichCell.seperatorLine setHidden:YES];
        textSwtichCell.switchController.on = self.setting.Shake;
    } else {
        [textSwtichCell.seperatorLine setHidden:YES];
        textSwtichCell.switchController.on = self.setting.Disturb;
    }
    [textSwtichCell.titleLabel setText:[self titleName:indexPath]];
    
    cell = textSwtichCell;
    
    return cell;
}

-(NSString *)titleName:(NSIndexPath *)indexPath
{
    NSString *title = nil;
    
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                    title = @"声音";
                    break;
                case 1:
                    title = @"振动";
                    break;
                default:
                    break;
            }
        }
            break;
        case 1:
        {
            title = @"勿扰模式";
        }
            break;
        default:
            break;
    }
    return title;
}




#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    CGFloat height = 0;
    switch (section) {
        case 1:
            height = KSECTIONHEIGHT;
            break;
        default:
            break;
    }
    return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 0;
    switch (section) {
        case 1:
            height = KSECTIONHEIGHT;
            break;
        default:
            break;
    }
    return height;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
{
    UIView *headerView = nil;
    if(section == 0)
    {
        return headerView;
    }
    headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, KSECTIONHEIGHT);
    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    UIView *beginLine = [[UIView alloc] init];
    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:beginLine];
    
    UIView *endLine = [[UIView alloc] init];
    endLine.frame = CGRectMake(0, KSECTIONHEIGHT-0.5, tableView.frame.size.width, 0.5);
    [endLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:endLine];
    
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
{
    UIView *headerView = nil;
    if(section != 1)
    {
        return headerView;
    }
    headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 30);
    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    UIView *beginLine = [[UIView alloc] init];
    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:beginLine];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, tableView.frame.size.width-15, 20)];
    [label setFont:[UIFont systemFontOfSize:12.0f]];
    [label setTextColor:RGBCOLOR(0x92, 0x92, 0x92)];
    [label setText:@"开启后，将自动屏蔽23:00－8:00的任何提醒"];
    [headerView addSubview:label];
    
    return headerView;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 50.0f;
    
    if(indexPath.row == 0 && indexPath.section == 0)
    {
        height = 50.0f;
    }
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.section) {
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                    break;
                case 1:
                    break;
                default:
                    break;
            }
        }
            break;
        case 2:
        {
            [self showSettingVC];
        }
            break;
        case 3:
        {
            [self showAboutVC];
        }
            break;
        default:
            break;
    }
    
    
}

- (void)SwitchCellDidChangeSettingWithIndexPath:(NSIndexPath *)indexPath settingValue:(BOOL)settingValue
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    if (indexPath.section == 0 && indexPath.row == 0) {
        self.setting.Voice = settingValue;
        
    } else if (indexPath.section == 0 && indexPath.row == 1) {
        self.setting.Shake = settingValue;
    } else {
        self.setting.Disturb = settingValue;
    }

    if (self.setting.Voice == 1) {
        dict[@"sound"] = @"yes";
    } else {
         dict[@"sound"] = @"no";
    }
    if (self.setting.Shake == 1) {
        dict[@"vibration"] = @"yes";
    } else {
       dict[@"vibration"] = @"no";
    }
    if (self.setting.Disturb == 1) {
        dict[@"noDisturb"] = @"yes";
        dict[@"noDisturbStart"] = @"23:00";
        dict[@"noDisturbEnd"] = @"08:00";

    } else {
        dict[@"noDisturb"] = @"no";
        dict[@"noDisturbStart"] = @"23:00";
        dict[@"noDisturbEnd"] = @"08:00";
    }
    if (self.setting.BothOnline == 1) {
        dict[@"sameOnline"] = @"yes";
    } else {
        dict[@"sameOnline"] = @"no";
    }
    [[CXmppRequestMgr shareInstance] SetUserSettingInfoWithSettingStr:[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding]];
}


- (void)saveSetting
{
    [[[CUserSettingMgr alloc] init] updateUserSetting:self.setting];

    [_tableView reloadData];
}
-(void)showAboutVC
{
    
}

-(void)showSettingVC
{
    
}

@end
